import com.hanxu.dao.BlogMapper;
import com.hanxu.pojo.Blog;
import com.hanxu.utils.IDUtils;
import com.hanxu.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.*;

public class Mytest {

    @Test
    public void tst(){
        System.out.println(IDUtils.getId());
    }

    @Test
    public void addblog(){
        SqlSession sqlSession = MybatisUtils.getSqlsession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);

        Blog blog = new Blog();
        blog.setId(IDUtils.getId());
        blog.setAuthor("houdongun");
        blog.setCreateTime(new Date());
        blog.setViews(999);
        blog.setTitle("first");

        blogMapper.addBlog(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("second");
        blogMapper.addBlog(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("third");
        blogMapper.addBlog(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("forth");
        blogMapper.addBlog(blog);

        sqlSession.close();
    }

    @Test
    public void queryBlogIF(){
        SqlSession sqlsession = MybatisUtils.getSqlsession();

        BlogMapper mapper = sqlsession.getMapper(BlogMapper.class);

        /**
         * 动态sql 语句 map 没有值得时候 查询全部 ，有的话就按照查询规则查询
         */
        HashMap map = new HashMap();
        map.put("title","second");
        map.put("author","houdongun");
        List<Blog> blogs = mapper.queryBlogIF(map);

        for (Blog blog : blogs) {
            System.out.println(blog);
        }

        sqlsession.close();
    }

    /**
     * for-eache
     */
    @Test
    public void queryBlogForeach(){
        SqlSession sqlSession = MybatisUtils.getSqlsession();
        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();

        ArrayList<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(3);
        map.put("ids",ids);

        List<Blog> list = blogMapper.queryBlogForeach(map);

        for (Blog blog : list) {
            System.out.println(blog);
        }

        sqlSession.close();
    }
}
